	var _url = '';
	
	function showRemoveHouse() {
        $('#removeHouseDiv').window('open');
        scrollVisible("removeHouseDiv");
        $('#removeHouseDiv').window('move',{top:$(document).scrollTop() + 200});
    }
	
	function showModifyHouse() {
        $('#modifyHouseDiv').window('open');
        scrollVisible("modifyHouseDiv");
        $('#modifyHouseDiv').window('move',{top:$(document).scrollTop() + 200});
            
    		var bedroomSum = $('#bedroomSum').val();
    		var data = $('#shiP').combobox('getData');
    		for(var i=0; i<data.length;i++) {
    			if(data[i].id==bedroomSum){
    				$('#shiP').combobox('select',data[i].id);
    			}
    		}
    		
    		var livingRoomSum = $('#livingRoomSum').val();
    		var data = $('#tingP').combobox('getData');
    		for(var i=0; i<data.length;i++) {
    			if(data[i].id==livingRoomSum){
    				$('#tingP').combobox('select',data[i].id);
    			}
    		}
    		
    		var wcSum = $('#wcSum').val();
    		var data = $('#weiP').combobox('getData');
    		for(var i=0; i<data.length;i++) {
    			if(data[i].id==wcSum){
    				$('#weiP').combobox('select',data[i].id);
    			}
    		}
    		
			var houseType = $('#houseType').val();
    		if(houseType == 2) {
        		var aboveFiveYear = $('#aboveFiveYear').val();
        	    $('input[name="manwu"]').each(function(){
        	    	if (aboveFiveYear != '' && $(this).val() == aboveFiveYear) {
                        $(this).attr("checked",'checked');
        				return;
                    } else if((aboveFiveYear == '' || aboveFiveYear == 0) && $(this).val() == 2){
                    	$(this).attr("checked",'checked');
        				return;
                    }
        	    });
        	    
        	    var onlyOne = $('#onlyOne').val();
        	    $('input[name="weiyi"]').each(function(){
        	    	if (onlyOne != '' && $(this).val() == onlyOne) {
                        $(this).attr("checked",'checked');
        				return;
                    } else if ((onlyOne == '' || onlyOne == 0) && $(this).val() == 2) {
                        $(this).attr("checked",'checked');
        				return;
                    }
        	    });
    		}    
        	getPriceRegion();
    		
    }
	
	function scrollVisible(id) {
        $("#" + id).window({
            closed:false,
            modal:true,
            onOpen: function () {
                $(document.body).css({
                    "overflow-x":"hidden",
                    "overflow-y":"hidden"
                });
                return true;
            },
            onClose: function () {
                $(document.body).css({
                    "overflow-x":"auto",
                    "overflow-y":"auto"
                });
                return true;
            }
        });
    }
   
	function removeHouse(houseId) {
        var value = $('input[name="reason"]:checked').val();
        if (value == null || value == "") {
            Iw.Util.errorNoty("请选择下架原因");
            return;
        }
        
        var memo = $('#r_memo').textbox("getValue");
        if(value == 0 && memo.length == 0) {
        	Iw.Util.errorNoty("请输入下架原因");
        	return;
        }
        if(value == 0 && memo.length > 100) {
        	Iw.Util.errorNoty("下架原因最多100个字");
        	return;
        }
        var dataObj = {"reason": $('input[name="reason"]:checked').val(), "memo": memo};
		var orderId = $('#orderId').val();
		var houseType = $('#houseType').val();
		var mobile = $('#houstMobile').val();
        jQuery("#removeHouseBtn").attr("disabled", "disabled");
        jQuery.ajax({
            "dataType": "JSON",
            "type": "POST",
            "url": _url + "removeHouse.json?houseId=" + houseId + "&orderId=" + orderId + "&houseType=" + houseType + "&isold=false" + "&mobile=" + mobile,
            "dataType": "json",
            "data": dataObj,
            "success": function (result) {
                if (result != null && result.code == "1") {
                    $('#removeHouseDiv').window('close');
					/*var referer = result.referer;
					if(referer == null || referer == "") {
						referer = "${basePath}/iw400/list.do";
					}
                    window.location.href = referer;*/
                    
                    window.opener && window.opener.doQuery();
	            	window.close();
                } else {
                    Iw.Util.errorNoty(result.message);
                    jQuery("#removeHouseBtn").removeAttr("disabled");
                }
            }
        });
    }
	
	//获取价格范围
    function getPriceRegion() {
    	var cityId = jQuery('#cityId').val();
    	var houseType = jQuery('#houseType').val();
    	var params = {'ht' : houseType, 'cityId' : cityId};
    	jQuery.ajax({
            url:_url + "getPriceRegion.json",
            data:params,
            type:"post",
            dataType:"json",
            async:false,
            success:function(result) {
                if (result.code == 1) {
    				$('#minPrice').val(result.data.minPrice);
                    $('#maxPrice').val(result.data.maxPrice);
                    return;
                } 
            }
        });
    } 
	
	//表单验证
    function validatePannelForm() {
    	var houseType = jQuery('#houseType').val();
        var shoujia = jQuery("#price").numberbox("getValue");
    	var maxPrice = $("#maxPrice").val();
    	if(maxPrice != "" && houseType == 2) {
    		 maxPrice = maxPrice / 10000 ;
    	}
    	var minPrice = $("#minPrice").val();
    	if(minPrice != "" && houseType == 2) {
    		minPrice = minPrice / 10000 ;
    	}
    	
        var mianji = jQuery("#mianji").textbox("getValue");
        var shiP = jQuery("#shiP").combobox("getValue");
        var tingP = jQuery("#tingP").combobox("getValue");
        var weiP = jQuery("#weiP").combobox("getValue");
    	
    	if (shiP == "") {
            jQuery.messager.alert("错误", "请选择室数!", "error");
            return false;
        }
        if (tingP == "") {
            jQuery.messager.alert("错误", "请选择厅数!", "error");
            return false;
        }
        if (weiP == "") {
            jQuery.messager.alert("错误", "请选择卫数!", "error");
            return false;
        }
    	
    	if (mianji == "") {
            jQuery.messager.alert("错误", "请输入面积!", "error");
            return false;
        }
    	
        if (!/^\d+(.\d{1,2})?$/.test(mianji)) {
            jQuery.messager.alert("错误", "面积请输入数字，请勿超过两位小数!", "error");
            return false;
        }

        if(mianji < 0 || mianji > 10000 ){
        	jQuery.messager.alert("错误", "面积区间在：0-10000平米", "error");
            return false;
        }
    	
    	if (shoujia == "") {
    		if(houseType == 2) {
    			jQuery.messager.alert("错误", "请输入正确的售价!", "error");
    		} else {
    			jQuery.messager.alert("错误", "请输入正确的租金!", "error");
    		}
            
            return false;
        }
    	shoujia = parseInt(shoujia);
    	if (maxPrice != "" && minPrice != "" && (shoujia > maxPrice || shoujia < minPrice)) {
    		if(houseType== 2) {
    			jQuery.messager.alert("错误", "售价区间在：" + minPrice + "-" + maxPrice + " 万元", "error");
    		} else {
    			jQuery.messager.alert("错误", "租金区间在：" + minPrice + "-" + maxPrice + " 元/月", "error");
    		}
    		
            return false;
    	}
    	
        return true;
    }
    
    function takePannelFormData() {
		var houseId = jQuery('#houseId').val();
    	var houseType = jQuery('#houseType').val();
    	var orderId = jQuery('#orderId').val();
        
        var price = jQuery("#price").numberbox("getValue");
        var mianji = jQuery("#mianji").textbox("getValue");
        
        var shiP = jQuery("#shiP").combobox("getValue");
        var tingP = jQuery("#tingP").combobox("getValue");
        var weiP = jQuery("#weiP").combobox("getValue");
        var fangdongname = jQuery("#hostName").val();
        var fangdongphone = jQuery("#houstMobile").val();
    	
    	var aboveFiveYear = jQuery("#aboveFiveYear").val();
    	var onlyOne = jQuery("#onlyOne").val();

    	if(jQuery("input[name='manwu']").length > 0) {
		    jQuery.each(jQuery("input[name='manwu']"), function(){
		       if ($(this).is(":checked")) {
		    	   aboveFiveYear = $(this).val();
		           return;
		       }
		    });
		    jQuery.each(jQuery("input[name='weiyi']"), function(){
		        if ($(this).is(":checked")) {
		        	onlyOne = $(this).val();
		            return;
		        }
		    });
    	}
    	var memo = jQuery("#memo").textbox("getValue");
    	
    	
        return {houseId : houseId, houseType : houseType, orderId : orderId, price : price, spaceArea : mianji, bedroomSum : shiP,
            livingRoomSum : tingP, wcSum : weiP, hostName : fangdongname, houstMobile : fangdongphone, aboveFiveYear : aboveFiveYear, 
            onlyOne : onlyOne, memo : memo};
    }
	
	function modifyHouse(id) {
        var flag = validatePannelForm();
        if (!flag)
            return;
        jQuery("#submitForm").linkbutton("disable");
    	var params = takePannelFormData();

    	jQuery.ajax({
            url:_url + "modifyHouse.json",
            data:params,
            type:"post",
            dataType:"json",
            async:false,
            success:function(result) {
                if (result != null && result.code == "1") {
                    $('#modifyHouseDiv').window('close');
                    
                    window.opener && window.opener.doQuery();
                  //window.close();
					var referer = result.referer;
					if(referer == null || referer == "") {
						referer = _url + "newdetail.do?orderId=" + id;
					}
                    window.location.href = referer;
                } else {
                    Iw.Util.errorNoty(result.message);
                    jQuery("#modifyHouseBtn").removeAttr("disabled");
                }
            }
        });
    }